import axios from 'axios'
import config from '../config'

export function getScheduleTaskAssignOrg () {
  return axios.get(config.SERVER_URL + window.apiPath + '/organizas-s?query=level==1', {responseType: 'json'})
    .then(resp => resp.data)
}

export function findAll (query = '') {
  return axios.get(config.SERVER_URL + window.apiPath + '/organizas-s?query=' + query, {responseType: 'json'})
    .then(resp => {
      let data = resp.data
      let level1 = data.filter(d => d.level < 2)
      let level2 = data.filter(d => d.level === 2)
      level2.forEach(d => {
        let p = level1.find(l1 => l1.id === d.parentId)
        if (p) {
          if (!p.sub) {
            p.sub = []
          }
          p.sub.push(d)
        }
      })
      return {data: level1, dataAll: resp.data}
    })
}

export function findFlat () {
  return axios.get(config.SERVER_URL + window.apiPath + '/organizas', {responseType: 'json'})
    .then(resp => resp.data)
}

export function findById (id) {
  return axios.get(config.SERVER_URL + window.apiPath + '/organizas/' + id, {responseType: 'json'})
    .then(resp => resp.data)
}

export function findByBusinessOrg (businessOrg) {
  let queryStr = ''
  if (businessOrg === 0) {
    queryStr = 'businessOrg==0;level==1'
  } else if (businessOrg === 1) {
    queryStr = 'businessOrg==1;'
  } else if (businessOrg === 2) {
    queryStr = 'businessOrg==2'
  } else if (businessOrg === 3) {
    queryStr = 'businessOrg==3'
  }
  return axios.get(config.SERVER_URL + window.apiPath + '/organizas-s', {params: {query: queryStr}}, {responseType: 'json'})
    .then(resp => resp.data)
}
//
// export function search (query) {
//   return axios.get(config.SERVER_URL + window.apiPath + '/organizas-s?query=' + query, {responseType: 'json'})
//     .then(resp => resp.data)
// }
